<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="藏品名称" prop="collectionName">
        <el-input
          v-model="queryParams.collectionName"
          placeholder="请输入藏品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品作者" prop="collectionAuthorId">
        <el-input
          v-model="queryParams.collectionAuthorId"
          placeholder="请输入藏品作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品数量" prop="collectionNum">
        <el-input
          v-model="queryParams.collectionNum"
          placeholder="请输入藏品数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="藏品价格  如果是俱乐部展示状态，则为藏品表最高价格"
        prop="collectionPrice"
      >
        <el-input
          v-model="queryParams.collectionPrice"
          placeholder="请输入藏品价格  如果是俱乐部展示状态，则为藏品表最高价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预售时间" prop="collectionTime">
        <el-input
          v-model="queryParams.collectionTime"
          placeholder="预售时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品分类 [数组 string]" prop="collectionAssort">
        <el-input
          v-model="queryParams.collectionAssort"
          placeholder="请输入藏品分类 [数组 string]"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['show:show:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['show:show:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['show:show:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['show:show:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="showList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="藏品出售展示id" align="center" prop="collectionShowId" />
      <el-table-column label="藏品名称" align="center" prop="collectionName" />
      <el-table-column label="藏品图片" align="center" prop="collectionUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.collectionUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="藏品作者" align="center" prop="collectionAuthorId" />
      <el-table-column
        label="藏品描述"
        align="center"
        prop="collectionDesc"
        show-overflow-tooltip="true"
      />
      <el-table-column label="藏品数量" align="center" prop="collectionNum" />
      <el-table-column label="藏品状态" align="center" prop="collectionShowStatus" />
      <el-table-column
        label="藏品价格  如果是俱乐部展示状态，则为藏品表最高价格"
        align="center"
        prop="collectionPrice"
      />
      <el-table-column label="预售时间" align="center" prop="collectionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="藏品分类 [数组 string]"
        align="center"
        prop="collectionAssort"
      />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['show:show:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['show:show:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改藏品出售展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1080px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="藏品名称" prop="collectionName">
          <el-input v-model="form.collectionName" placeholder="请输入藏品名称" />
        </el-form-item>
        <el-form-item label="藏品图片">
          <image-upload v-model="form.collectionUrl" />
        </el-form-item>
        <el-form-item label="藏品作者" prop="collectionAuthorId">
          <el-input v-model="form.collectionAuthorId" placeholder="请输入藏品作者" />
        </el-form-item>
        <el-form-item label="藏品描述" prop="collectionDesc">
          <el-input
            v-model="form.collectionDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="藏品数量" prop="collectionNum">
          <el-input v-model="form.collectionNum" placeholder="请输入藏品数量" />
        </el-form-item>
        <el-form-item
          label="藏品价格  如果是俱乐部展示状态，则为藏品表最高价格"
          prop="collectionPrice"
        >
          <el-input
            v-model="form.collectionPrice"
            placeholder="请输入藏品价格  如果是俱乐部展示状态，则为藏品表最高价格"
          />
        </el-form-item>
        <el-form-item label="藏品状态  " prop="collectionShowStatus">
          <el-input
            v-model="form.collectionShowStatus"
            placeholder="藏品状态 0正常 1预售 3俱乐部 4活动"
          />
        </el-form-item>
        <el-form-item label="预售时间" prop="collectionTime">
          <el-input v-model="form.collectionTime" placeholder="请输入预售时间" />
        </el-form-item>
        <el-form-item label="藏品分类 [数组 string]" prop="collectionAssort">
          <el-input
            v-model="form.collectionAssort"
            placeholder="请输入藏品分类 [数组 string]"
          />
        </el-form-item>
        <el-divider content-position="center">藏品详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddSysCollection"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteSysCollection"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="sysCollectionList"
          :row-class-name="rowSysCollectionIndex"
          @selection-change="handleSysCollectionSelectionChange"
          ref="sysCollection"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="藏品链上id" prop="collectionHashId" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.collectionHashId"
                placeholder="请输入藏品链上id"
              />
            </template>
          </el-table-column>
          <el-table-column label="藏品名称" prop="collectionName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.collectionName" placeholder="请输入藏品名称" />
            </template>
          </el-table-column>
          <el-table-column label="藏品价格" prop="collectionPrice" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.collectionPrice"
                placeholder="请输入藏品价格"
              />
            </template>
          </el-table-column>
          <el-table-column label="藏品状态" prop="collectionStatus" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.collectionStatus"
                placeholder="请输入藏品状态"
              />
            </template>
          </el-table-column>
          <el-table-column label="藏品图片地址" prop="collectionUrl" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.collectionUrl"
                placeholder="请输入藏品图片地址"
              />
            </template>
          </el-table-column>
          <el-table-column label="藏品作者" prop="collectionAuthorId" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.collectionAuthorId"
                placeholder="请输入藏品作者"
              />
            </template>
          </el-table-column>
          <el-table-column label="拥有者id" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入拥有者id" />
            </template>
          </el-table-column>
          <el-table-column label="是否出售 0未出售 1已出售" prop="isSell" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.isSell"
                placeholder="请输入是否出售 0未出售 1已出售"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShow, getShow, delShow, addShow, updateShow } from "@/api/show/show";

export default {
  name: "Show",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysCollection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 藏品出售展示表格数据
      showList: [],
      // 藏品详情表格数据
      sysCollectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectionName: null,
        collectionUrl: null,
        collectionAuthorId: null,
        collectionDesc: null,
        collectionNum: null,
        collectionShowStatus: null,
        collectionPrice: null,
        collectionTime: null,
        collectionAssort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collectionName: [
          { required: true, message: "藏品名称不能为空", trigger: "blur" },
        ],
        collectionUrl: [{ required: true, message: "藏品图片不能为空", trigger: "blur" }],
        collectionAuthorId: [
          { required: true, message: "藏品作者不能为空", trigger: "blur" },
        ],
        collectionDesc: [
          { required: true, message: "藏品描述不能为空", trigger: "blur" },
        ],
        collectionNum: [{ required: true, message: "藏品数量不能为空", trigger: "blur" }],
        collectionShowStatus: [
          { required: true, message: "藏品状态不能为空", trigger: "blur" },
        ],
        collectionPrice: [
          {
            required: true,
            message: "藏品价格  如果是俱乐部展示状态，则为藏品表最高价格不能为空",
            trigger: "blur",
          },
        ],
        collectionTime: [
          { required: true, message: "预售时间不能为空", trigger: "blur" },
        ],
        collectionAssort: [
          { required: true, message: "藏品分类 [数组 string]不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询藏品出售展示列表 */
    getList() {
      this.loading = true;
      listShow(this.queryParams).then((response) => {
        this.showList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        collectionShowId: null,
        collectionName: null,
        collectionUrl: null,
        collectionAuthorId: null,
        collectionDesc: null,
        collectionNum: null,
        collectionShowStatus: "0",
        collectionPrice: null,
        collectionTime: null,
        collectionAssort: null,
      };
      this.sysCollectionList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.collectionShowId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加藏品出售展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const collectionShowId = row.collectionShowId || this.ids;
      getShow(collectionShowId).then((response) => {
        this.form = response.data;
        this.sysCollectionList = response.data.sysCollectionList;
        this.open = true;
        this.title = "修改藏品出售展示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.sysCollectionList = this.sysCollectionList;
          if (this.form.collectionShowId != null) {
            updateShow(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShow(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const collectionShowIds = row.collectionShowId || this.ids;
      this.$modal
        .confirm('是否确认删除藏品出售展示编号为"' + collectionShowIds + '"的数据项？')
        .then(function () {
          return delShow(collectionShowIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 藏品详情序号 */
    rowSysCollectionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 藏品详情添加按钮操作 */
    handleAddSysCollection() {
      let obj = {};
      obj.collectionHashId = "";
      obj.collectionName = "";
      obj.collectionPrice = "";
      obj.collectionStatus = "";
      obj.collectionUrl = "";
      obj.collectionAuthorId = "";
      obj.collectionDesc = "";
      obj.userId = "";
      obj.isSell = "";
      this.sysCollectionList.push(obj);
    },
    /** 藏品详情删除按钮操作 */
    handleDeleteSysCollection() {
      if (this.checkedSysCollection.length == 0) {
        this.$modal.msgError("请先选择要删除的藏品详情数据");
      } else {
        const sysCollectionList = this.sysCollectionList;
        const checkedSysCollection = this.checkedSysCollection;
        this.sysCollectionList = sysCollectionList.filter(function (item) {
          return checkedSysCollection.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleSysCollectionSelectionChange(selection) {
      this.checkedSysCollection = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "show/show/export",
        {
          ...this.queryParams,
        },
        `show_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
